package middleware.llvm.instruction;

import middleware.llvm.value.Value;

import java.io.PrintStream;

/**
 * 加载指令，从内存中加载数据
 *
 * @author 杨凯伟
 * @date 2024/10/30
 */
public class LoadInstruction extends Instruction {

    Value ptr;


    public Value getPtr() {
        return ptr;
    }

    public void setPtr(Value ptr) {
        this.ptr = ptr;
    }

    public LoadInstruction(Value ptr) {
        super(ptr.getType().clone().ptr(ptr.getType().getPtrNum() - 1), ptr);
        this.ptr = ptr;
    }

    @Override
    public void replaceOperand(int pos, Value newOperand) {
        super.replaceOperand(pos, newOperand);
        if (pos == 0) {
            ptr = newOperand;
        }
    }

    @Override
    public void dump(PrintStream out) {
        var dataType = ptr.getType().clone().ptr(ptr.getType().getPtrNum() - 1);
        out.printf("  %s = load %s, %s\n", getName(), dataType.toString(), ptr.toString());
    }


}
